/**
 * Copyright (c) 2015-2020, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jiot.common.serviceGenerator;

import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.dialect.*;
import com.jfinal.plugin.activerecord.generator.MetaBuilder;
import com.jfinal.plugin.activerecord.generator.TableMeta;
import com.jiot.plugin.HikariCP.HikariCPPlugin;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import cn.hutool.core.util.StrUtil;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 代码生成工具类
 */
public class CodeGenHelpler {


    /**
     * 获取数据源
     *
     * @return
     */
    public static DataSource getDatasource() {
    	Prop p = PropKit.useFirstFound("demo-config-pro.txt", "demo-config-dev.txt");
//		DruidPlugin druidPlugin = new DruidPlugin(p.get("jdbcUrl"), p.get("user"), p.get("password").trim());
		HikariCPPlugin dbPool = new HikariCPPlugin(p.get("jdbcUrl"), p.get("user"), p.get("password").trim(), p.get("driverClassName").trim());
		
		dbPool.start();
		return dbPool.getDataSource();
    }


    public static MetaBuilder createMetaBuilder() {
        MetaBuilder metaBuilder = new MetaBuilder(getDatasource());
        metaBuilder.setGenerateRemarks(true);
        metaBuilder.setDialect(new MysqlDialect());
        
        return metaBuilder;

    }


    /**
     * 排除指定的表，有些表不需要生成的
     *
     * @param list
     * @param excludeTables
     */
    public static void excludeTables(List<TableMeta> list, String excludeTables) {
        if (StrUtil.isNotBlank(excludeTables)) {
            List<TableMeta> newTableMetaList = new ArrayList<>();
//            String[] excludeTableSet = StrUtil.split(excludeTables.toLowerCase(), ",");
            for (TableMeta tableMeta : list) {
//                if (excludeTableSet.contains(tableMeta.name.toLowerCase())) {
//                    System.out.println("exclude table : " + tableMeta.name);
//                    continue;
//                }
                newTableMetaList.add(tableMeta);
            }
            list.clear();
            list.addAll(newTableMetaList);
        }
    }


}


